import { h } from 'snabbdom';

function renderTitleBlack(elemNode, children, editor) {
    const attributes = elemNode.allAttributes || {};

    // 如果是 table 元素，直接渲染为 table
    if (elemNode.type === 'table') {
        return renderTable(elemNode, children, editor);
    }

    const processedChildren = children.map(child => {
        if (child && child.type === 'mention') {
            return renderTitleBlack(child, child.children || [], editor);
        } else if (child && typeof child === 'object') {
            return child;
        } else {
            return child;
        }
    });

    const vnode = h(
        elemNode.myTagName || 'div',
        {
            attrs: attributes,
        },
        processedChildren
    );
    return vnode;
}

function renderTable(elemNode, children, editor) {
    const attributes = elemNode.allAttributes || {};

    const rows = children.map(row => {
        const rowChildren = row.children.map(cell => {
            return h(
                'td', 
                { attrs: {} }, 
                [cell.children[0]?.text || '']
            );
        });
        return h('tr', {}, rowChildren);
    });

    return h('table', { attrs: attributes }, rows);
}

export const renderElemConf = {
    type: 'mention',
    renderElem: renderTitleBlack,
};